<template>
  <view class="content">
    <button class="button" type="primary" @click="initConfig()">初始化定位配置(非必须)</button>
    <button class="button" type="primary" @click="startOnceLocation(false)">单次定位(不要地址信息)</button>
    <button class="button" type="primary" @click="startOnceLocation(true)">单次定位(要地址信息)</button>
    <button class="button" type="primary" @click="startContinuousLocation(true, false)">连续定位</button>
    <button class="button" type="primary" @click="startContinuousLocation(true, true)">后台定位</button>
    <button class="button" type="primary" @click="stopUpdatingLocation()">关闭定位</button>
    <button class="button" type="primary" @click="clickAlive()">保活(仅限Android)</button>
    <button class="button" type="primary" @click="clickUnAlive()">取消保活(仅限Android)</button>
  </view>
</template>

<script>
// 首先需要通过 uni.requireNativePlugin("ModuleName") 获取 module
var testModule = uni.requireNativePlugin('LY-DCLocationAlive-LocationAliveModule');

export default {
  data() {
    return {
      locationCount: 0,
      errorCount: 0
    };
  },
  onLoad() {

  },
  methods: {
    // 初始化定位配置
    initConfig: function() {
      testModule.initLocationConfig({
        locationTimeout: 3,// 最低值2秒, 默认10秒
        reGeocodeTimeout: 3,// 最低值2秒, 默认10秒
        locationInterval: 3,// Android 最低1s, iOS 高德定位SDK不支持直接设置间隔
      });
    },
    // 单次定位
    startOnceLocation: function(isGeocode) {
      testModule.requestLocationWithReGeocode(isGeocode, ret => {
          uni.showToast({
            title: `单次定位成功, 经度:${ret.latitude}, 纬度:${ret.longitude}`,
            icon: "none"
          })
        }
      );
    },
    // 持续定位
    startContinuousLocation: function(regeocode, allowBackground) {
      testModule.startUpdatingLocation(regeocode, allowBackground, ret => {
        if (-1 == ret.code) {
          this.locationCount++;
          uni.showToast({
            title: `第${this.locationCount}次定位成功, 经度:${ret.latitude}, 纬度:${ret.longitude}`,
            icon: "none"
          })
          console.log("具体地址:" + ret.address.formattedAddress);
        } else {
          this.errorCount ++;
          uni.showToast({
            title: `第${this.errorCount}次定位失败: ${ret.errMsg}`,
            icon: "none"
          })
        }
      })
    },
    // 关闭定位
    stopUpdatingLocation: function() {
      this.locationCount = 0;
      testModule.stopUpdatingLocation();
    },
    // 保活
    clickAlive: function() {
      testModule.keepAlive(ret=>{
        if (-1 == ret.code) {
          this.locationCount++;
          uni.showToast({
            title: `第${this.locationCount}次定位成功, 经度:${ret.latitude}, 纬度:${ret.longitude}`,
            icon: "none"
          })
          console.log(ret.address.formattedAddress);
        } else {
          this.errorCount ++;
          uni.showToast({
            title: `第${this.errorCount}次定位失败: ${ret.errMsg}`,
            icon: "none"
          })
        }
      });
    },
    // 取消保活
    clickUnAlive: function() {
      testModule.nonKeepAlive();
    },
  }
};
</script>

<style lang="scss">
page {
  background-color: #ffffff;
}

.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #ffffff;
}

.button {
  width: 750rpx;
  height: 100rpx;
  line-height: 100rpx;
  color: white;
  background-color: dodgerblue;
}
</style>
